package cn.peng.stu4teach.pages.funcs;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import cn.peng.stu4teach.R;
import cn.peng.stu4teach.databinding.ItemFuncEditBinding;
import cn.peng.stu4teach.databinding.ItemFuncEditTitleBinding;
import cn.peng.stu4teach.pages.home.bean.FuncsBean;

/**
 * “全部功能” 中的 Adapter
 */
public class FuncsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * 条目类型-功能
     */
    private final int                  TYPE_FUNC  = 0x00000001;
    /**
     * 条目类型-分组标题
     */
    private final int                  TYPE_TITLE = 0x00000002;
    /**
     * 外部的 ViewModel
     */
    private final AllFuncsVm           mVm;
    /**
     * 要呈现的列表数据
     */
    private       ArrayList<FuncsBean> items      = new ArrayList<>();
    /**
     * 用于区域是哪个 Rv 调用了这个适配器
     */
    private String mRvType ;

    public FuncsAdapter (AllFuncsVm pVm,String rvType) {
        mVm = pVm;
        mRvType = rvType;
    }

    public void setItems (ArrayList<FuncsBean> pItems) {
        items = pItems;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE_FUNC:
                ItemFuncEditBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_func_edit, parent, false);
                return new FuncItemVH(binding);
            case TYPE_TITLE:
                ItemFuncEditTitleBinding titleBinding = DataBindingUtil.inflate(inflater, R.layout.item_func_edit_title, parent, false);
                return new TitleItemVH(titleBinding);

            default:
                throw new IllegalArgumentException("Unknown viewType: " + viewType);
        }
    }

    @Override
    public void onBindViewHolder (@NonNull RecyclerView.ViewHolder holder, int position) {
        // 功能条目
        if (holder instanceof FuncItemVH) {
            FuncItemVH itemHolder = (FuncItemVH) holder;
            if (null == itemHolder.mBinding || position > items.size()) {
                // 避免空指针和索引越界
                return;
            }
            int realPos = itemHolder.getAdapterPosition();
            FuncsBean item = items.get(realPos);
            itemHolder.mBinding.setItem(item);
            itemHolder.mBinding.setVm(mVm);

            View view = itemHolder.mBinding.getRoot();
            itemHolder.mBinding.getRoot().setOnClickListener((pView) -> mVm.onItemClick(view, item, realPos,mRvType));
            return;
        }

        // 分组标题条目
        if (holder instanceof TitleItemVH) {
            TitleItemVH itemHolder = (TitleItemVH) holder;
            if (null == itemHolder.mBinding || position > items.size()) {
                // 避免空指针和索引越界
                return;
            }
            int realPos = itemHolder.getAdapterPosition();
            FuncsBean item = items.get(realPos);
            itemHolder.mBinding.setItem(item);
        }
    }

    @Override
    public int getItemViewType (int position) {
        super.getItemViewType(position);

        FuncsBean funcItem = items.get(position);
        return funcItem.isGroupTitle ? TYPE_TITLE : TYPE_FUNC;
    }

    /**
     * 有多少个条目
     */
    @Override
    public int getItemCount () {
        return items.size();
    }

    /**
     * ViewHolder 便于复用
     */
    class FuncItemVH extends RecyclerView.ViewHolder {
        final ItemFuncEditBinding mBinding;

        public FuncItemVH (@NonNull ItemFuncEditBinding pBinding) {
            super(pBinding.getRoot());
            mBinding = pBinding;
        }
    }

    /**
     * ViewHolder 便于复用
     */
    class TitleItemVH extends RecyclerView.ViewHolder {
        final ItemFuncEditTitleBinding mBinding;

        public TitleItemVH (@NonNull ItemFuncEditTitleBinding pBinding) {
            super(pBinding.getRoot());
            mBinding = pBinding;
        }
    }
}
